# import requests
# from lxml import etree
# url='http://www.baidu.com'
#
# r=requests.get(url)
# r.encoding=r.apparent_encoding
# y=r.text
# doc=etree.HTML(y)
# ccc=doc.xpath('//input[@id="su"]/@value')[0]
# print(ccc)
import requests
from lxml import etree
import csv
import pandas
def get_html(url):
    head = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36"}
    try:
        r=requests.get(url=url,headers=head)
        r.encoding=r.apparent_encoding
        r.raise_for_status()
        return r.text
    except Exception as e:
        print(e)
def parser(html):
    doc = etree.HTML(html)
    title=doc.xpath('//div[@id="rankWrap"]/div[2]/ul/li/a/@title')
    href=doc.xpath('//div[@id="rankWrap"]/div[2]/ul/li/a/@href')
    out_dict={'歌名':title,'地址':href}
    return out_dict
def save_csv(path,content):
    df =pandas.DataFrame(content)
    df.to_csv(path)
if __name__ == '__main__':
    url = 'http://www.bspoder.top/kugou/'
    yuanma=get_html(url)
    shuju=parser(yuanma)
    lujing='d\\kugou.csv'
    save_csv(lujing,shuju)